﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using Newtonsoft.Json;
using CoreFrame.Core.Orm;


public class OwnerController : AbstractJqueryController
{
    public void GetSexData()
    {
        TxtJson = "[{\"Id\":-1,\"Name\":\"未知性别\"},{\"Id\":0,\"Name\":\"男性\"},{\"Id\":1,\"Name\":\"女性\"}]";
    }

    #region 添加人员
    /// <summary>
    /// 添加人员
    /// </summary>
    public void AddPerson()
    {
        try
        {
            AbstractOwner owner = NewObject<AbstractOwner>();
            int edittype = Convert.ToInt32(FormData["edittype"]); //0:操作房间 1：操作门面
            int addtype = Convert.ToInt32(FormData["addtype"]); //0:新增业主 1：新增业主成员
            string roomid = Convert.ToString(FormData["RoomId"]);

            GetModelToForm<AbstractOwner>(owner);
            if (owner.Save(edittype,addtype, roomid))
            {
                TxtJson = ReturnSuccess("维护人员成功！");
            }
            else
            {
                TxtJson = ReturnSuccess("维护人员失败！");
            }
        }
        catch(Exception ex)
        {
            TxtJson = ReturnError(ex.Message.ToString());
        }
    }
    #endregion


    #region 获取业主关联人员列表
    /// <summary>
    /// 获取业主关联人员列表
    /// </summary>
    public void GetOwnerRelPer()
    {
        string personid = Convert.ToString(ParamsData["personid"]);
        if (personid==null||personid == "")
        {
            TxtJson = "{\"total\":" + 0 + ",\"rows\": []}";
        }
        else
        {
            AbstractOwner owner = NewObject<AbstractOwner>();
            List<AbstractOwner> ownerList = owner.GetOwnerList(personid);
            TxtJson = ListToJson(ownerList);
        }
    }
    #endregion



    #region 查找人员
    /// <summary>
    /// 查找人员
    /// </summary>
    public void GetOwnerList()
    {
        AbstractOwner owner = NewObject<AbstractOwner>();
        int pagesize = Convert.ToInt32(ParamsData["page"]);
        int rows = Convert.ToInt32(ParamsData["rows"]);
        string personName ="";
        string cardnum ="";
        string mobile ="";

        if (ParamsData.ContainsKey("PersonName"))
        {
            personName = Convert.ToString(ParamsData["PersonName"]);
        }
        if (ParamsData.ContainsKey("Cardnum"))
        {
            cardnum = Convert.ToString(ParamsData["Cardnum"]);
        }
        if (ParamsData.ContainsKey("Mobile"))
        {
            mobile = Convert.ToString(ParamsData["Mobile"]);
        }
        PageInfo page = new PageInfo(rows, pagesize);
        List<AbstractOwner> ownerList = owner.GetOwnerList(page, personName, cardnum, mobile);

        string value = "";
        string child = "";
        for (int IntI = 0; IntI < ownerList.Count; IntI++)
        {
            child = JavaScriptConvert.SerializeObject(ownerList[IntI], new AspNetDateTimeConverter());
            child = child.Substring(child.IndexOf("{") + 1);
            value += "{\"id\":\"" + ownerList[IntI].OwnerId + "\",\"_parentId\":\"" + ownerList[IntI].POwnerId + "\"," + child + ",";
        }
        value = value.Remove(value.LastIndexOf(","));
        TxtJson = "{\"total\":" + page.totalRecord + ",\"rows\":[" + value + "]}";
    }
    #endregion


    #region 删除人员
    /// <summary>
    /// 删除人员
    /// </summary>
    public void DelPerson()
    {
        string ownerId = Convert.ToString(ParamsData["OwnerId"]);
        AbstractOwner owner = NewObject<AbstractOwner>();
        List<AbstractOwner> ownerList = owner.GetOwnerList(ownerId);
        if (ownerList.Count > 0)
        {
            TxtJson = ReturnSuccess("该业主还有未删除的关联人员！");
            return;
        }
        if (owner.Delete(ownerId))
        {
            TxtJson = ReturnSuccess("删除人员成功！");
        }
        else
        {
            TxtJson = ReturnSuccess("删除人员失败！");
        }
    }
    #endregion  

    #region 获取门面
    /// <summary>
    /// 获取门面
    /// </summary>
    public void GetShopList()
    {
        string shopname = ParamsData["shopname"];
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractShop shop = NewObject<AbstractShop>();
        System.Data.DataTable dt = shop.GetShopAndOwnerList(shopname, page);
        TxtJson = base.DataTableToJson(dt, page.totalRecord);
    }
    #endregion
}
 
